package com.example.demo.config.recode;

import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.SecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.DefaultSecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.stereotype.Component;

@Component
public class ReCodeAuthenticationSecurityConfig extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> {
    @Autowired
    ReCodeManage reCodeManage;
    @Autowired
    UserService userService;
    @Override
    public void configure(HttpSecurity http) throws Exception {
        String appid = "wx7b956fb274d35669";
        ReCodeAuthenticationFilter authenticationFilter = new ReCodeAuthenticationFilter( appid );
        authenticationFilter.setReCodeManage(  reCodeManage);
        authenticationFilter.setAuthenticationManager( http.getSharedObject(AuthenticationManager.class) );
        ReCodeAuthenticationProvider reCodeAuthenticationProvider = new ReCodeAuthenticationProvider();
        reCodeAuthenticationProvider.setUserService( userService );
        http.authenticationProvider(reCodeAuthenticationProvider)
                .addFilterBefore(authenticationFilter, UsernamePasswordAuthenticationFilter.class)
                .addFilterAfter(authenticationFilter, UsernamePasswordAuthenticationFilter.class);
    }

}
